package com.example.cloud.service;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.example.cloud.model.UserInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;

/**
 * 异步请求
 *
 * @author tongyufu
 */
@Service
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 异步请求
     *
     * @param id id
     * @return userInfo
     */
    @HystrixCommand
    public Future<UserInfo> getUserByIdAsync(final Integer id) {
        return new AsyncResult<UserInfo>() {
            @Override
            public UserInfo invoke() {
                String url = "http://HELLO-SERVICE/user/{1}";
                return restTemplate.getForObject(url, UserInfo.class, id);
            }
        };
    }

}
